using DGZImp.Core.Configuration;
using DGZImp.Core.Extensions;
using DGZImp.Sys.Controllers.Hubs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using DGZImp.Core;
using DGZImp.Core.Language;
using Autofac;
using Microsoft.Extensions.DependencyModel;
using System.Linq;
using DGZImp.Core.BaseProvider;
using Microsoft.Extensions.FileProviders;
using System.IO;
using System;
using DGZImp.MdsProject;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;
using DGZImp.WebApi.Services;

namespace DGZImp.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            Configuration = configuration;
            Env = env;
        }

        public IConfiguration Configuration { get; }
        public IWebHostEnvironment Env { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCoreService(Env, Configuration);
            services.AddTransient<EmailService>();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            app.UseCoreLanguagePack();

            app.UseCoreConfigure(env, Configuration);

            var appPath = Path.Combine(AppContext.BaseDirectory, "..", "app");
            if (!Directory.Exists(appPath)) Directory.CreateDirectory(appPath);

            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(appPath),
                //配置访问虚拟目录时文件夹别名
                RequestPath = "/app",
                ServeUnknownFileTypes = true,
                DefaultContentType = "application/octet-stream"
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                //配置SignalR
                if (AppSetting.UseSignalR)
                {
                    string corsUrls = Configuration["CorsUrls"];
                    if (!string.IsNullOrEmpty(corsUrls))
                    {
                        endpoints.MapHub<HomePageMessageHub>("/message").RequireCors(t =>
                            t.WithOrigins(corsUrls.Split(',')).AllowAnyMethod().AllowAnyHeader().AllowCredentials()
                        );
                    }
                }
            });
        }
    }

    /// <summary>
    /// Swagger注释帮助类
    /// </summary>
    public class SwaggerDocTag : IDocumentFilter
    {
        /// <summary>
        /// 添加附加注释
        /// </summary>
        /// <param name="swaggerDoc"></param>
        /// <param name="context"></param>
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            //添加对应的控制器描述
            swaggerDoc.Tags = new List<OpenApiTag>
            {
                new OpenApiTag { Name = "Test", Description = "这是描述" },
                //new OpenApiTag { Name = "你的控制器名字，不带Controller", Description = "控制器描述" },
            };
        }
    }
}
